# 无标题窗口+label+webbrower+拖动

from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QWidget  
from PyQt5.QtGui import QPainter, QPainterPath, QPen, QBrush, QColor, QRegion  
from PyQt5.QtCore import Qt, QPoint, QRectF, pyqtSlot, QEvent  
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
  
class MainWindow(QMainWindow):  
    def __init__(self):  
        super().__init__()  
        self.initUI()  
  
    def initUI(self):  
        # 设置窗口标题（注意：虽然是无边框窗口，但标题仍然可以设置）  
        self.setWindowTitle("PyQt5 Desktop QMainWindow") 

        self.setWindowFlags(Qt.FramelessWindowHint)  # 设置无边框窗口  

        self.setAttribute(Qt.WA_TranslucentBackground)  # 设置窗口背景透明  
        # 圆角半径  
        self.radius = 20

        # # 设置窗口大小  
        # self.resize(800, 600)  
        # 设置初始大小和位置（可选）  
        self.setGeometry(100, 100, 800, 600)  
  
        # 使窗口居中显示  
        screen = QApplication.primaryScreen()  
        screen_rect = screen.geometry()  
        self.move(screen_rect.center() - self.rect().center())  
  
        # layout = QVBoxLayout()
        # layout.addWidget(QLabel("Hello, World!"))
        # self.setLayout(layout)
        # QMainWindow，是中央布局不能直接这样了，
        # 而要改成:设置中央部件和布局  
        central_widget = QWidget(self)  
        self.setCentralWidget(central_widget)  
        label = QLabel("PyQt5 Desktop QMainWindow!",central_widget)
        # 设置字体和颜色（使用样式表）  
        label.setStyleSheet("""  
            QLabel {  
                font-family: Arial;  
                font-size: 14pt;  
                font-weight: bold;  
                color: green;
                background-color: rgba(0, 0, 200, 200); 
            }  
        """) 
        label.setGeometry(10, 5, self.width() - 20, 35)
        
        # 创建一个QWebEngineView实例  
        self.browser = QWebEngineView(self)
        # 加载网页  
        self.browser.load(QUrl("https://www.baidu.com"))  
        # 设置QWebEngineView的大小以适应窗口内部（不包括圆角部分）  
        # 假设圆角的半径为10  
        self.browser.setGeometry(10, 40, self.width() - 20, self.height() - 50)
  
    def paintEvent(self, event):  
        painter = QPainter(self)  
        painter.setRenderHint(QPainter.Antialiasing)  
  
        # 绘制圆角矩形背景  
        pen = QPen(Qt.NoPen)  
        brush = QBrush(QColor(255, 255, 255, 255))  # 设置背景颜色  
        painter.setPen(pen) 
        painter.setBrush(brush)  
        rect = self.rect()  
        rect.adjust(5, 5, -5, -5)  # 根据需要调整圆角大小  
        path = QPainterPath()  
        path.addRoundedRect(QRectF(rect) , 10, 10)  # 圆角半径为10  
        painter.drawPath(path)
  
    def mousePressEvent(self, event):  
        # 当鼠标按下时记录位置，用于后续拖动窗口  
        self.drag_position = event.globalPos() - self.frameGeometry().topLeft()  
  
    def mouseMoveEvent(self, event):  
        # 当鼠标移动时，如果左键被按下，则移动窗口  
        if event.buttons() == Qt.LeftButton:  
            self.move(event.globalPos() - self.drag_position)  
            event.accept()  
  
# 示例使用  
if __name__ == '__main__':  
    import sys  
    app = QApplication(sys.argv)  
    mainWindow = MainWindow()  
    mainWindow.show()  
    sys.exit(app.exec_())